import { routerMapp } from "@/router"
import { RouterView } from "vue-router";
import layout from "@/components/layout/layout.vue";
import { shallowRef } from 'vue'
export const permission={
    state:() =>({
        routers: routerMapp,
        addRouters: []
    }),

    actions:{
        GenerateRoutes({ commit }, asyncRouter) {
            commit('SET_ROUTERS', asyncRouter)
        }
    },
    getters:{
        routers: state => state.routers,
        addRouters: state => state.addRouters,
    },
    mutations:{
        SET_ROUTERS: (state, routers) => {
            state.addRouters = routers
            state.routers = routerMapp.concat(routers)
        }
    }
}
/**
 * 动态路由解析
 * @param routerList
 * @returns {*}
 */
export const resolveRouter=(routerList)=>{
    return routerList.filter(router=>{
        if (router.component) {
            if (router.component === 'Layout') { // Layout组件特殊处理
                router.component = shallowRef(layout)
            } else {
                const component = router.component
                router.component = loadView(component)
            }
        }
        if (router.children && router.children.length) {
            router.children = resolveRouter(router.children)
        }
        return true
    })
}


/**
 * 获取所有页面
 * @param view
 * @returns {function(*): any}
 */
export const loadView=(view)=>{
    const viteModules = import.meta.glob('@/views/*/*/*.vue');
    let newPath='/src/views/'+view+'.vue'
    return viteModules[newPath]
}
